package code;

import java.util.ArrayList;

import main.ErrorException;
import define.CodeOut;
import define.ReferenceExtraType;
import excelBase.Sheet;

public class FlexOut extends BaseOut 
{
	public FlexOut(boolean isAllowSet, String nameSpace) throws ErrorException
	{
		super(isAllowSet, nameSpace, CodeOut.E_FLEX_CO);
	}
	
	protected StringBuffer outMember(ReferenceMember member, int tabNumber)	throws ErrorException 
	{
		StringBuffer outBuffer = new StringBuffer("");
		String comment = member.getComment();
		String name = member.getName();
		member.setTypeText(outType(member.getType(), m_codeOut));
		if(comment.length() != 0)
		{
			outBuffer.append("//" + comment + ENDL);
		}
		outBuffer.append("private var ");
		outBuffer.append("m_" + name + ":" + member.getTypeText() + ";" + ENDL);
		return outBuffer;
	}

	protected StringBuffer outMethod(ReferenceMember member, int tabNumber)	throws ErrorException 
	{
		String name = member.getName();
		StringBuffer outBuffer = new StringBuffer("");
		outBuffer.append("public function");
		outBuffer.append(" get" + member.getNameFirstUp() + "():" + member.getTypeText() + ENDL + "{" + ENDL);
		outBuffer.append("return m_" + name + ";" + ENDL);
		outBuffer.append("}" + ENDL + ENDL);
		if(m_isAllowSet)
		{
			outBuffer.append("public function set" + member.getNameFirstUp() + "(");
			outBuffer.append(name + ":" + member.getTypeText() + "):void" + ENDL + "{" + ENDL);
			outBuffer.append("m_" + name + " = " + name + ";" + ENDL);
			outBuffer.append("}" + ENDL + ENDL);
		}
		return outBuffer;
	}
	
	protected StringBuffer outExtraType(ReferenceExtraType ret,	String childrenText) throws ErrorException 
	{
		StringBuffer buf = new StringBuffer(ret.getCodeValue(m_codeOut));
		if(ret == ReferenceExtraType.E_ARRAY_RET || ret == ReferenceExtraType.E_MAP_RET)
		{
		}
		else
		{
			buf.append(ret.getStart() + childrenText + ret.getEnd());
		}
		return buf;
	}

	protected StringBuffer outClass(ReferenceClass rc) throws ErrorException {
		StringBuffer out = new StringBuffer("");
		String name = rc.getName();
		if(m_nameSpace != null)
		{
			out.append("package " + m_nameSpace + ENDL + "{" + ENDL);
		}
		out.append("public class " + name + ENDL + "{" + ENDL);
		ArrayList<ReferenceMember> list = rc.getMemberList();
		for(ReferenceMember mem: list)
		{
			out.append(outMember(mem, 2));
		}
		out.append(ENDL + "public function " + name + "(");
		for(int i = 0, max = list.size() - 1; i <= max; ++i)
		{
			ReferenceMember mem = list.get(i);
			out.append(mem.getName() + ":" + mem.getTypeText() + mem.getDefault());
			if(i != max)
			{
				out.append(", ");
			}
			else
			{
				out.append(")" + ENDL + "{" + ENDL); 
			}
		}
		for(int i = 0, max = list.size() - 1; i <= max; ++i)
		{
			ReferenceMember mem = list.get(i);
			out.append("m_" + mem.getName() + " = " + mem.getName() + ";");
			if(i != max)
			{
				out.append(ENDL);
			}
			else
			{
				out.append(ENDL + "}" + ENDL + ENDL); 
			}
		}
		for(ReferenceMember mem: list)
		{
			out.append(outMethod(mem, 2));
		}
		out.append("}" + ENDL + "}");
		return out;
	}

	public void save(String path, int position) throws ErrorException
	{
		for(Sheet temp: m_referenceList)
		{
			String name = path + temp.getName() + ".as";
			saveToFile(name, outClass(temp.getReferenceClass()), position);
		}
	}

}
